﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;

namespace Jy123.Web.Blog
{
    public partial class BlogDetail1 : System.Web.UI.Page
    {
        private int userID;
        protected void Page_Load(object sender, EventArgs e)
        {
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);

            string strBlogID = Request.QueryString["BlogID"];
            int blogID = 0;
            if (!string.IsNullOrEmpty(strBlogID))
            {
                blogID = Convert.ToInt32(strBlogID);
            }
            else
            { blogID = Convert.ToInt32(Request["hidBlogID"]); }

            string type = Request.QueryString["Type"];
            if (type == "del")
            {
                string commentID = Request.QueryString["CommentID"];
                new BLL.Blog_Comment().Delete(Convert.ToInt32(commentID));
            }
            //提交评论
            string strSubmit = Request["btnSubmit"];
            if (!string.IsNullOrEmpty(strSubmit))
            {
                Jy123.Model.Blog_Comment mBlogComment = new Jy123.Model.Blog_Comment();
                Jy123.BLL.Blog_Comment bllBlogComment = new Jy123.BLL.Blog_Comment();
                mBlogComment.FBlog = blogID;
                mBlogComment.FUserCommenter = userID;
                mBlogComment.BlogCommContent = Request["txtComment"];
                mBlogComment.PostTime = DateTime.Now;
                mBlogComment.PostIP = Common.Public.GetClientIP();
                mBlogComment.IsLock = false;
                int addCommentID = bllBlogComment.Add(mBlogComment);
                if (addCommentID > 0)
                {
                    //加入动态
                    Jy123.Model.User_Main mUserForState = new Jy123.Model.User_Main();
                    Jy123.Model.User_Main mUserAuthorForState = new Jy123.Model.User_Main();
                    Jy123.BLL.User_Main bllUserForState = new Jy123.BLL.User_Main();
                    mUserForState = bllUserForState.GetModel(userID);//访问者
                    mUserAuthorForState=bllUserForState.GetModel(new Jy123.BLL.User_Blog().GetModel(blogID).FUser);//日志作者
                    
                    Jy123.Model.User_Blog mBlog = new Jy123.Model.User_Blog();
                    Jy123.BLL.User_Blog bllBlog = new Jy123.BLL.User_Blog();
                    mBlog = bllBlog.GetModel(blogID);
                    mBlogComment = bllBlogComment.GetModel(addCommentID);
                    new Jy123.BLL.Public.State().CreateCommentBlogState(mUserForState,mUserAuthorForState,mBlog,mBlogComment);
                    //加入动态
                }
            }
            //阅读数+1
            Jy123.Model.User_Blog mUserBlog = new Jy123.Model.User_Blog();
            Jy123.BLL.User_Blog bllUserBlog = new Jy123.BLL.User_Blog();
            mUserBlog = bllUserBlog.GetModel(blogID);
            mUserBlog.Reads += 1;
            bllUserBlog.Update(mUserBlog);


            //主页面
            StringBuilder sbHtml = new StringBuilder(Cache["user_blogdetail"].ToString());

            //加载被处理后的页面
            pageLoad(sbHtml, blogID);

            Response.ContentType = "text/plain";
            Response.Write(sbHtml.ToString());
        }
        private void pageLoad(StringBuilder sbHtml, int blogID)
        {
            //头部、底部、左侧用户命令菜单
            sbHtml = sbHtml.Replace("$Header$", Cache["user_header2"].ToString());
            sbHtml = sbHtml.Replace("$Footer$", Cache["user_footer"].ToString());
            sbHtml = sbHtml.Replace("$LeftUserCmd$", Cache["user_navigation_down"].ToString());
            sbHtml = sbHtml.Replace("$LeftUserFriendAdd$", Cache["user_friendadd"].ToString());
            sbHtml.Replace("$FaceList$", new Jy123.BLL.User.UserSql().FaceList(Cache["user_face"].ToString()));//初始化表情列表

            //日志详细
            Jy123.Model.MPagination mpag = new Jy123.Model.MPagination();
            mpag.Tables = "V_User_BlogList";
            mpag.PrimaryKey = "ID";
            mpag.Fields = "*";
            mpag.CurrentPage = 1;
            mpag.PageSize = int.MaxValue;
            mpag.Filter = "ID='" + blogID + "'";
            DataRow drBlogList = Jy123.BLL.Public.Pagination.GetList(mpag).Tables[0].Rows[0];
            sbHtml = sbHtml.Replace("$BeAccessUserName$", drBlogList["UserName"].ToString());
            sbHtml = sbHtml.Replace("$BlogPostTime$", drBlogList["PostTime"].ToString());
            sbHtml = sbHtml.Replace("$BlogTitle$", drBlogList["Title"].ToString());

            sbHtml = sbHtml.Replace("$BlogContent$", drBlogList["BlogContent"].ToString());
            sbHtml = sbHtml.Replace("$BlogReads$", drBlogList["Reads"].ToString());
            sbHtml = sbHtml.Replace("$BlogComments$", drBlogList["Comments"].ToString());
            sbHtml = sbHtml.Replace("$hidBlogID$", blogID.ToString());
            sbHtml = sbHtml.Replace("$UserID$", userID.ToString());
            //统计用户拥有的日志数量
            int blogCount = new BLL.User.Blog().BlogCount(userID);
            sbHtml = sbHtml.Replace("$BlogCount$", blogCount.ToString());
            #region 日志评论列表
            string strBlogComment = Cache["user_blogdetail_Comment_li"].ToString();
            StringBuilder sbBlogComment = new StringBuilder();
            Jy123.BLL.User.Blog bllBlogComment = new Jy123.BLL.User.Blog();
            DataTable dtBlogList = bllBlogComment.BlogDetailComment(1, int.MaxValue, "FBlog = '" + blogID + "'").Tables[0];
            //string strDel = @"<img id=""CommentIDDel"" src=""/images/del_blog.gif"" alt=""删除"" url=""javascript:window.location('/Blog/BlogDetail.aspx?Type=del&BlogID=$BlogID$&CommentID=$CommentID$');""/>";
            string strDel = @"<a href=""/Blog/BlogDetail.aspx?Type=del&BlogID=$BlogID$&CommentID=$CommentID$"" class=""imgdel"" >
        <img src=""/images/del_blog.gif"" alt=""删除"" /></a>";


            for (int i = 0; i < dtBlogList.Rows.Count; i++)
            {
                //根据用户ID判断用户是否有头像,如果没有头像就返回默认头像地址
                string userPortrait = new Jy123.BLL.User.SPace().DefaultPortrait(Convert.ToInt32(dtBlogList.Rows[i]["FUserCommenter"]));

                string str = strBlogComment;
                if (dtBlogList.Rows[i]["FUserCommenter"].ToString().Equals(userID.ToString()))
                {
                    str = str.Replace("$imgdel$",strDel);
                }
                else { str = str.Replace("$imgdel$", string.Empty); }

                str = str.Replace("$CommentID$", dtBlogList.Rows[i]["ID"].ToString());
                str = str.Replace("$BlogID$",blogID.ToString());
                //str = str.Replace("$ID$", dtBlogList.Rows[i]["ID"].ToString());
                str = str.Replace("$PostTime$", dtBlogList.Rows[i]["PostTime"].ToString());
                str = str.Replace("$UserPortrait$", userPortrait);
                str = str.Replace("$UserName$", dtBlogList.Rows[i]["UserName"].ToString());
                str = str.Replace("$BlogCommContent$", Jy123.BLL.User.UserSql.BlogContent(dtBlogList.Rows[i]["BlogCommContent"].ToString()));
                str = str.Replace("$FUserCommenter$", dtBlogList.Rows[i]["FUserCommenter"].ToString());

                sbBlogComment.Append(str);
            #endregion
            }
            sbHtml = sbHtml.Replace("$user_blogdetail_Comment_li$", sbBlogComment.ToString());
        }
        
    }
}
